<template>
  <!--项目备案start -->
  <el-dialog
    v-dialogDrag
    :title="dialogTitle"
    :close-on-click-modal="false"
    :visible.sync="dialogVisible"
    class="dialogStyleMiddle"
    :before-close="cancel"
  >
    <el-form
      ref="addForm"
      class="dialog-form"
      :model="formData"
      label-width="80px"
      :rules="rules"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="" prop="recordTime">
            <template slot="label">
              <span class="labelTitle">备案日期</span>
            </template>
            <el-date-picker
              v-model="formData.recordTime"
              style="width:100%"
              size="medium"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              clearable
              @change="recordTimeChange"
            />

          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备案地区" prop="regaionList">
            <el-cascader
              v-model="formData.regaionList"
              size="medium"
              placeholder="请选择所在地区"
              :options="cityOptions"
              clearable
              style="width: 100%"
              @change="handleRegion"
            >
              <template slot-scope="{ data }">
                <span>{{ data.label }}</span>
              </template>
            </el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="formData.address" clearable size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备案说明" prop="recordExplain">
            <el-input v-model="formData.recordExplain" clearable type="textarea" :rows="3" size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备案证明" prop="appFiles">
            <FileUplode
              ref="fileUplode"
              :edit-file-list="filingList"
              :status="status"
              @getFileList="getFiling"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <DialogFooter />
    </div>
  </el-dialog>
  <!--项目备案end -->
</template>

<script>
// 页面接口
import { listEnterpriseRecordAdd, listEnterpriseRecordUpdate, listEnterpriseRecordDetail } from '@/api/system/EnterpriseFiling'
// 上传文件
import FileUplode from '@/components/Upload/FileUplode'
// dialog-footer
import DialogFooter from '@/components/Common/Btns/DialogFooter/index'
export default {
  components: {
    FileUplode,
    DialogFooter
  },
  props: {
    cityOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      /* 新增 start*/
      dialogTitle: '', // 弹层标题
      dialogVisible: false, // 弹层是否显示
      formData: { // 新增数据
        id: '', // 项目id
        recordType: '0',
        recordTime: '', // 备案时间
        recordStartDate: '',
        recordEndDate: '',
        regaionList: '',
        address: '',
        recordExplain: '',
        appFiles: ''
      },
      rules: {
        recordType: [{ required: true, message: '备案类型不能为空', trigger: 'change' }],
        recordTime: [{ required: true, message: '开始日期不能为空', trigger: 'change' }],
        regaionList: [{ required: true, message: '备案地区不能为空', trigger: 'change' }],
        address: [{ required: true, message: '详情地址不能为空', trigger: 'blur' }],
        appFiles: [{ required: true, message: '备案证明不能为空', trigger: 'blur' }]
      },
      status: '',
      /* 新增 end*/
      readOnly: true,
      filingList: []// 附件
    }
  },
  methods: {
    // 备案日期变化
    recordTimeChange(e) {
      this.formData.recordStartDate = e[0]
      this.formData.recordEndDate = e[1]
    },
    handleRegion(regionList) {
      const [nation, region, province, city, county] = regionList
      this.formData = { ...this.formData, nation, region, province, city, county }
    },
    /* 新增/编辑弹层 start*/
    // 新增/编辑
    handleAdd(title, id) {
      this.dialogTitle = title
      this.dialogVisible = true
      if (id) {
        this.getDetailById(id)
      }
    },
    // 查询备案的详情
    getDetailById(id) {
      listEnterpriseRecordDetail(id).then(res => {
        if (res.data) {
          const { nation, region, province, city, county, appFiles } = res.data
          this.formData = { ...this.formData, ...res.data }
          this.formData.regaionList = [nation, region, province, city, county]
          if (res.data.recordStartDate && res.data.recordEndDate) this.formData.recordTime = [this.formData.recordStartDate, this.formData.recordEndDate]
          this.filingList = appFiles
        }
      })
    },
    // 取消按钮
    cancel() {
      this.formData.id = ''
      this.resetForm('addForm')
      this.filingList = []
      this.formData.regaionList = []
      this.dialogVisible = false
    },
    // 提交按钮
    submitForm() {
      this.$refs['addForm'].validate(async valid => {
        if (valid) {
          if (this.formData.id) {
            // 修改
            await listEnterpriseRecordUpdate(this.formData)
            this.msgSuccess('修改成功')
          } else {
            // 新增
            await listEnterpriseRecordAdd(this.formData)
            this.msgSuccess('保存成功')
          }
          this.cancel()
          this.$parent.getList()
        }
      })
    },
    /* 新增弹层 end*/
    /** ************ 文件上传start *******************/
    getFiling(fileList, deleteFileIds) {
      this.formData.appFiles = fileList
    }
    /** ************ 文件上传end *******************/
  }

}
</script>

<style>

</style>
